﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using Microsoft.Phone.Tasks;
using System.IO;
using LocalSettings;
using System.Windows.Media.Imaging;
using QuickTasks.ViewModels.Data;
 
namespace QuickTasks
{
    public partial class AddPeople : PhoneApplicationPage
    {
        #region Global

        IsolatedStorageFile isostore = IsolatedStorageFile.GetUserStoreForApplication();
        IsolatedStorageFileStream isostream = null;
        XDocument xdoc = null;
        string favPeopleId = "";
        PhoneNumberChooserTask phoneNumberChooserTask;
        EmailAddressChooserTask emailAddressChooserTask;
        string favid="";
        string cont = "";
        string type = "";
        #endregion

        #region constructor

        public AddPeople()
        {
            InitializeComponent();
        }

        #endregion

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (NavigationContext.QueryString.TryGetValue("Cont",out cont))
            {
                ApplicationTitle.Text = "QUICK TASKS - ADD CONTACT";
                type = "";
                if (App.contactslist1 != null)
                {
                    foreach (var conpeople in App.contactslist1)
                    {
                        tbxName.Text = conpeople.contactname;
                        if (conpeople.mphonenum != null)
                        tbxmobile.Text = conpeople.mphonenum;
                        if (conpeople.hphonenum !=null)
                        tbxHome.Text = conpeople.hphonenum;
                        if (conpeople.wphonenum != null)
                        tbxWork.Text = conpeople.wphonenum;
                        if (conpeople.pemail != null)
                        tbxEmailId.Text = conpeople.pemail;
                        break;
                    }
                }
            }
           
            if (NavigationContext.QueryString.TryGetValue("favPeopleId", out favPeopleId))
            {
                ApplicationTitle.Text = "QUICK TASKS - EDIT CONTACT";
                type = "Edit";
                favid = NavigationContext.QueryString["favPeopleId"];
                List<FavoritePeopleProp> objfavoritePeople = new List<FavoritePeopleProp>();
                if (isostore.FileExists("QuickTasks/People.xml"))
                {
                    isostream = new IsolatedStorageFileStream("QuickTasks/People.xml", FileMode.Open, isostore);
                    xdoc = XDocument.Load(isostream);
                    isostream.Close();

                    var favpeoplelist = from i in xdoc.Descendants("people") where i.Attribute("id").Value == favPeopleId select i;
                    foreach (var favpeople in favpeoplelist)
                    {
                        tbxName.Text = favpeople.Element("name").Value;
                        tbxmobile.Text = favpeople.Element("mobile").Value;
                        tbxHome.Text = favpeople.Element("home").Value;
                        tbxWork.Text = favpeople.Element("work").Value;
                        tbxEmailId.Text = favpeople.Element("emailId").Value;
                        break;
                    }

                }
            }
           
                else
            {
                ApplicationTitle.Text = "QUICK TASKS - ADD CONTACT";
                type = "";
            }
            
        }

       


        #region events

        private void ApplicationBarsaveButton_Click(object sender, EventArgs e)
        {
            string exist=string.Empty;
            if (isostore.FileExists("QuickTasks/People.xml"))
            {
                isostream = new IsolatedStorageFileStream("QuickTasks/People.xml", FileMode.Open, isostore);
                xdoc = XDocument.Load(isostream);
                isostream.Close();

                var addressid = from i in xdoc.Descendants("people") where i.Element("mobile").Value.ToString() == tbxmobile.Text select i;

                foreach (var rep in addressid)
                {
                    if (type == "")
                      exist="true";
                }
            }
          
            if (tbxName.Text == "")
            {
                tblkNameValidation.Visibility = Visibility.Visible;
                return;
            }
            else
                tblkNameValidation.Visibility = Visibility.Collapsed;

            if (tbxmobile.Text == "")
            {
                tblkMobileValidation.Visibility = Visibility.Visible;
                return;
            }
            else
                tblkMobileValidation.Visibility = Visibility.Collapsed;
            if (exist == "true")
            {
                tblkMobileValidation1.Visibility = Visibility.Visible;
                return;
            }
            else
            {
                tblkMobileValidation1.Visibility = Visibility.Collapsed;
            }
            string mobileNo = tbxmobile.Text;

            

            if (tbxEmailId.Text != "")
            {
                if (!Regex.IsMatch(tbxEmailId.Text, @"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"))
                {
                    tblkEmailValidation.Text = "Please Enter Correct Id";
                    tblkEmailValidation.Visibility = Visibility.Visible;
                    return;
                }
            }
            else
            {
                tblkEmailValidation.Visibility = Visibility.Visible;
                tblkEmailValidation.Text = "Required";
                return;
            }

            FavoritePeople objFavotitepeople = new FavoritePeople();
            objFavotitepeople.SavePeople(tbxName.Text, tbxmobile.Text, tbxHome.Text, tbxWork.Text, tbxEmailId.Text, favid);
            NavigationService.GoBack();
        }

        private void ThemeableMobileChooseImage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            phoneNumberChooserTask = new PhoneNumberChooserTask();
            phoneNumberChooserTask.Completed += new EventHandler<PhoneNumberResult>(phoneNumberChooserTask_Completed);
            phoneNumberChooserTask.Show();
        }
        void phoneNumberChooserTask_Completed(object sender, PhoneNumberResult e)
        {

            if (e.TaskResult == TaskResult.OK)
            {
                tbxmobile.Text = e.PhoneNumber;
            }
        }
        private void ThemeableEmailChooseImage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            emailAddressChooserTask = new EmailAddressChooserTask();
            emailAddressChooserTask.Completed += new EventHandler<EmailResult>(emailAddressChooserTask_Completed);
            emailAddressChooserTask.Show();

        }

        void emailAddressChooserTask_Completed(object sender, EmailResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                tbxEmailId.Text = e.Email;
            }
        }

        #endregion

        private void ThemeableImage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            phoneNumberChooserTask = new PhoneNumberChooserTask();
            phoneNumberChooserTask.Completed += new EventHandler<PhoneNumberResult>(phoneNumberChooserTask1_Completed);
            phoneNumberChooserTask.Show();
        }
        void phoneNumberChooserTask1_Completed(object sender, PhoneNumberResult e)
        {

            if (e.TaskResult == TaskResult.OK)
            {
              tbxHome.Text = e.PhoneNumber;
            }
        }

        private void ThemeableImage_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
        {
            phoneNumberChooserTask = new PhoneNumberChooserTask();
            phoneNumberChooserTask.Completed += new EventHandler<PhoneNumberResult>(phoneNumberChooserTask2_Completed);
            phoneNumberChooserTask.Show();
        }
        void phoneNumberChooserTask2_Completed(object sender, PhoneNumberResult e)
        {

            if (e.TaskResult == TaskResult.OK)
            {
                tbxWork.Text = e.PhoneNumber;
            }
        }

        private void ThemeableMobileChooseImagecont_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/FavoritePeople/contacts.xaml?con=1", UriKind.Relative));
        }

       
      }
   }

